//
//  GraphBar.m
//  mworkingHaier
//
//  Created by Saborka on 27/12/2016.
//  https://github.com/Saborka/CustomGraphView
//

#import "GraphBar.h"


@interface GraphBar ()

@property (strong, nonatomic) CAShapeLayer *shapeLayer;

@end

@implementation GraphBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        //属性geometryFlipped 决定了一个图层的坐标是否相对于父图层且垂直翻转,设置为YES的话, 那么图层的排版就会沿着底部来排版
       // self.layer.geometryFlipped = YES;
        _shapeLayer = [CAShapeLayer layer];
       // _shapeLayer.lineCap      = kCALineCapButt;
        _shapeLayer.fillColor    = [[UIColor whiteColor] CGColor];
        _shapeLayer.strokeColor = [UIColor blueColor].CGColor;
        _shapeLayer.lineWidth    = 15;
        _shapeLayer.strokeEnd    = 0.f;
        _shapeLayer.lineCap = kCALineCapRound;
        _shapeLayer.lineJoin = kCALineCapRound;
        self.clipsToBounds = YES;//剪裁超出父视图范围的子视图部分 它取值为 NO 时，不剪裁子视图。
       // self.layer.masksToBounds =YES;
        /*
        clipsToBounds(UIView)
        是指视图上的子视图,如果超出父视图的部分就截取掉,
        masksToBounds(CALayer)
        却是指视图的图层上的子图层,如果超出父图层的部分就截取掉
         */
        [self.layer addSublayer:_shapeLayer];
    }
    return self;
}

- (void)setBarColor:(UIColor *)barColor
{
    _barColor = barColor;
    _shapeLayer.strokeColor = barColor.CGColor;
}

- (void)setBarValue:(CGFloat)barValue
{
    _barValue = barValue;
    
    UIBezierPath *barLine = [UIBezierPath bezierPath];
#if 0
    [barLine moveToPoint:CGPointMake(0, self.height)];
    [barLine addLineToPoint:CGPointMake(0, (1 - barValue) * self.height+self.width/2)];
    [barLine addQuadCurveToPoint:CGPointMake(self.width, (1 - barValue) * self.height+self.width/2) controlPoint:CGPointMake(self.width/2, (1 - barValue) * self.height)];
    [barLine addLineToPoint:CGPointMake(self.width, self.height)];
#endif
    
    [barLine moveToPoint:CGPointMake(self.width/2, self.height)];
    [barLine addLineToPoint:CGPointMake(self.width/2, (1 - barValue) * self.height)];
    
    [barLine setLineWidth:1.0];
    [barLine setLineCapStyle:kCGLineCapRound];
    _shapeLayer.path = barLine.CGPath;
    
    
    if (_barColor) {
        _shapeLayer.strokeColor = [_barColor CGColor];
    }
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 1.f;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnimation.toValue = [NSNumber numberWithFloat:1.f];
    pathAnimation.autoreverses = NO;
    [_shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    _shapeLayer.strokeEnd = 1.0;
    [self addXLabel];
}

//标记x轴label
- (void)addXLabel{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,(1 - _barValue) * self.height -30,self.width, 20)];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:12.f];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor =[UIColor colorFromHex:@"999999"];
    label.text =[NSString stringWithFormat:@"%.1f", _currentBarValue];
    [self addSubview:label];
}


- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
    CGContextFillRect(context, rect);
}

@end
